package com.begye.smartloan.apptest.huankuanbefore

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.AtPinHuanKuanProcessBinding
import com.begye.smartloan.apptest.ext.addUnderLine
import com.begye.smartloan.apptest.ext.toThousandUnitFormat
import com.begye.smartloan.apptest.gongong.YongHuJiDingYiViewModel
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.tool.addTouch
import com.util.utillib.extend.IntentFlagMode
import com.util.utillib.extend.toNextActivity
import java.util.Timer
import java.util.TimerTask

@SuppressLint("CustomSplashScreen")
class HuanKuanBeforeProcessingActivity : AppCompatActivity() {

    private lateinit var _binding: AtPinHuanKuanProcessBinding
    private var _pinTimer = Timer()

    private val _yongHuJiDingYiViewModel by lazy {
        ViewModelProvider(this)[YongHuJiDingYiViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtPinHuanKuanProcessBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configYongHuLiuChengState()
        configUIState()
        configUIEvent()
        configUIData()
    }

    private fun configYongHuLiuChengState() {
        _yongHuJiDingYiViewModel.onLoadingStateLiveData.observe(this) {
            if (it == true) {
                _binding.atGlobalLoading.show()
            } else {
                _binding.atGlobalLoading.hide()
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun configUIState() {
        _yongHuJiDingYiViewModel.onPinStatusLiveData.observe(this) {

        }

        _yongHuJiDingYiViewModel.onHuanKuanUrlLiveData.observe(this) {
            setCutDownStart()
        }
    }

    private fun configUIEvent() {

        _binding.atPinHuanKuanProcessNar.initConfig(
            text = getString(R.string.huan_kuan_text_35),
            textColor = "#000000",
            leftResId = R.drawable.dres_39,
            rightResId = R.drawable.dres_38
        ) {
            if (0 == it) {
                toNextActivity(
                    activity = HuanKuanBeforeActivity::class.java,
                    mode = IntentFlagMode.SINGLE_TOP
                )
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        _binding.atPinHuanKuanProcessRefresh.addUnderLine()

        _binding.atPinHuanKuanProcessRefresh.setOnClickListener {
            _yongHuJiDingYiViewModel.requestYongHuJiDingYiForHuanKuanPinStatus()
        }

        _binding.atPinHuanKuanProcessMakeAgain.setOnClickListener {
            val curText =
                _binding.atPinHuanKuanProcessMakeAgain.text.toString().trim()
            if (curText.contains("(") or curText.contains(")")) {
                return@setOnClickListener
            }

            _binding.atPinHuanKuanProcessMakeAgain.isEnabled = false
            _yongHuJiDingYiViewModel.requestYongHuJiDingYiForHuanKuanPinUrl()
        }
    }

    private fun configUIData() {
        val jinE = intent.getStringExtra("jinE") ?: ""
        val product = intent.getStringExtra("product") ?: ""
        _binding.atPinHuanKuanProcessJinEValue.text = jinE.toThousandUnitFormat()
        _binding.atPinHuanKuanProcessProductValue.text = product.toThousandUnitFormat()
        setCutDownStart()
    }

    @SuppressLint("SetTextI18n")
    private fun setCutDownFinished() {
        try {
            _pinTimer.cancel()

            _pinTimer.cancel()
            _binding.atPinHuanKuanProcessMakeAgain.setBackgroundResource(
                R.drawable.layer_c25_000000
            )
            _binding.atPinHuanKuanProcessMakeAgain.text = getString(R.string.huan_kuan_text_7)
            _binding.atPinHuanKuanProcessMakeAgain.isEnabled = true

        } catch (e: Exception) {
            //
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setCutDownStart() {
        var curTick = 60
        try {
            _pinTimer.cancel()
            _pinTimer = Timer()
            _pinTimer.schedule(object : TimerTask() {
                override fun run() {
                    runOnUiThread {
                        _binding.atPinHuanKuanProcessMakeAgain.isEnabled = false
                        _binding.atPinHuanKuanProcessMakeAgain.text =
                            getString(R.string.huan_kuan_text_7) + " (${curTick}s)"
                        _binding.atPinHuanKuanProcessMakeAgain.setTextColor(Color.parseColor("#ffffff"))
                        _binding.atPinHuanKuanProcessMakeAgain.setBackgroundResource(R.drawable.layer_c25_cfcdd2)

                        curTick -= 1
                        if (curTick == 0) {
                            _pinTimer.cancel()
                            _binding.atPinHuanKuanProcessMakeAgain.setBackgroundResource(
                                R.drawable.layer_c25_000000
                            )

                            _binding.atPinHuanKuanProcessMakeAgain.setTextColor(Color.parseColor("#BEFF6A"))
                            _binding.atPinHuanKuanProcessMakeAgain.text =
                                getString(R.string.huan_kuan_text_7)
                            _binding.atPinHuanKuanProcessMakeAgain.isEnabled = true
                        }
                    }
                }
            }, 0, 1000)
        } catch (e: Exception) {
            //
        }
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}